package com.dzl.admin.service.impl;

import com.dzl.admin.dao.SysMenuMapper;
import com.dzl.admin.pojo.SysMenu;
import com.dzl.admin.pojo.vo.SysMenuVo;
import com.dzl.admin.service.SysMenuService;
import com.dzl.base.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * (SysMenu)表服务实现类
 *
 * @author caobucao
 * @since 2021-05-13 15:45:47
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenuVo> queryMenuTree(Integer isEnable) {
        List<SysMenu> menuList=sysMenuMapper.queryAllMenu(isEnable);
        // 0：表示根节点的父ID
        List<SysMenuVo> resultList = transferMenuVo(menuList, 0);
        return resultList;
    }

    private List<SysMenuVo> transferMenuVo(List<SysMenu> allMenu, Integer parentId){
        List<SysMenuVo> resultList=new ArrayList<SysMenuVo>();
        if(!CollectionUtils.isEmpty(allMenu)){
            for(SysMenu source:allMenu){
                if(parentId.equals(source.getParentId())){
                    SysMenuVo menuVo=new SysMenuVo(source);
                    if(source.getParentId().equals(0)) {
                        menuVo.setRedirect("noredirect");
                    }
                    //递归查询子菜单，并封装信息
                    List<SysMenuVo> childList=transferMenuVo(allMenu,source.getId());
                    if(!CollectionUtils.isEmpty(childList)){
                        menuVo.setChildren(childList);
                    }
                    resultList.add(menuVo);
                }
            }
        }
        return resultList;
    }

    @Override
    public List<SysMenu> queryAllMenu() {
        return sysMenuMapper.queryAllMenu(null);
    }
}
